package com.longcai.piaojia.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.activity.ChooseCityActivity;
import com.longcai.piaojia.activity.GoodsInfoActivity;
import com.longcai.piaojia.adapter.RushedAdapter2;
import com.longcai.piaojia.adapter.ShowListAdapter;
import com.longcai.piaojia.conn.ShowListGet;
import com.longcai.piaojia.conn.ShowTypeListGet;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.fragment.AppV4Fragment;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.Http;
import com.zcx.helper.util.UtilToast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/8/2.
 */
public class ShowFragment extends AppV4Fragment implements View.OnClickListener {

    @BoundView(R.id.show_choose_city_ll)
    private LinearLayout show_choose_city_ll;
    @BoundView(R.id.show_choose_city_tv)
    private TextView show_choose_city_tv;
    @BoundView(R.id.show_tab_rv)
    private RecyclerView show_tab_rv;
    @BoundView(R.id.show_lv)
    private PullToRefreshListView pullToRefreshListView;
    private ListView show_lv;
    //    private ShowTabListAdapter showTabListAdapter;
    private RushedAdapter2 rushedAdapter2;
    private ShowListAdapter showListAdapter;
    private List<ShowTypeListGet.ShowTypeEntity> class_lists = new ArrayList<>();
    private List<ShowListGet.ShowEntity> show_lists = new ArrayList<>();
    public static boolean is_live = false;
    private SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm");
    private ShowListGet.ShowListEntity showList;

    String region_id;

    private ShowTypeListGet showTypeListGet = new ShowTypeListGet(new AsyCallBack<ShowTypeListGet.ShowTypeEntityList>() {

        @Override
        public void onStart(int type) throws Exception {
            super.onStart(type);

//            Http.getInstance().show(getActivity());

        }

        @Override
        public void onSuccess(String toast, int type, ShowTypeListGet.ShowTypeEntityList showTypeEntityList) throws Exception {

            ShowTypeListGet.ShowTypeEntity showTypeEntity = new ShowTypeListGet.ShowTypeEntity();

            showTypeEntity.id = "";

            showTypeEntity.title = getResources().getString(R.string.show_activity_all_type_text);

            class_lists.add(showTypeEntity);

            class_lists.addAll(showTypeEntityList.showTypeEntityList);

            if (class_lists.size() > 0) {

                showListGet.region_id = BaseApplication.BasePreferences.getCityId();
                showListGet.classid = class_lists.get(0).id;
                showListGet.page = 1;
                showListGet.execute(getActivity(), false, 0);

            }

            rushedAdapter2.notifyDataSetChanged(class_lists, 0);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {


            UtilToast.show(getActivity(), toast);
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {

//            Http.getInstance().dismiss();
        }
    });

    private ShowListGet showListGet = new ShowListGet(new AsyCallBack<ShowListGet.ShowListEntity>() {

        @Override
        public void onEnd(String toast, int type) throws Exception {
            super.onEnd(toast, type);

            if (type == 0) {

                Http.getInstance().dismiss();

            } else {

                pullToRefreshListView.onPullDownRefreshComplete();
                pullToRefreshListView.onPullUpRefreshComplete();
                setLastUpdateTime();

            }

        }

        @Override
        public void onSuccess(String toast, int type, ShowListGet.ShowListEntity showListEntity) throws Exception {
            super.onSuccess(toast, type, showListEntity);

            showList = showListEntity;

            show_lists.addAll(showListEntity.list);

            showListAdapter.notifyDataSetChanged();

            if (type == 0) {

                show_lv.setSelection(0);

            }

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(getActivity(), toast, Toast.LENGTH_LONG).show();

        }

    });

    private void setLastUpdateTime() {
        String text = formatDateTime(System.currentTimeMillis());
        pullToRefreshListView.setLastUpdatedLabel(text);
    }

    private String formatDateTime(long time) {
        if (0 == time) {
            return "";
        }

        return mDateFormat.format(new Date(time));
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        is_live = true;

        View view = BoundViewHelper.boundView(this, inflater.inflate(R.layout.fragment_show, null));

        show_choose_city_tv.setText(BaseApplication.BasePreferences.getCityName());

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        show_tab_rv.setLayoutManager(linearLayoutManager);

        rushedAdapter2 = new RushedAdapter2(getActivity(), class_lists);
        show_tab_rv.setAdapter(rushedAdapter2);

        pullToRefreshListView.setPullRefreshEnabled(true);
        pullToRefreshListView.setPullLoadEnabled(true);
        pullToRefreshListView.setScrollLoadEnabled(false);
        show_lv = pullToRefreshListView.getRefreshableView();
        show_lv.setDivider(null);
        show_lv.setVerticalScrollBarEnabled(false);

        showListAdapter = new ShowListAdapter(getActivity(), show_lists);
        show_lv.setAdapter(showListAdapter);

//        show_tab_hlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//
//
//            }
//        });

        rushedAdapter2.setOnItemClickListener(new RushedAdapter2.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

                if (rushedAdapter2.tabChooseMap.get(position) != true) {

                    rushedAdapter2.notifyDataSetChanged(class_lists, position);

                    show_lists.clear();

//                    Http.getInstance().show(getActivity());

//                    showListGet.region_id = mCity_id;
                    showListGet.region_id = BaseApplication.BasePreferences.getCityId();
                    showListGet.classid = class_lists.get(position).id;
                    showListGet.page = 1;
//                    showListGet.execute(getActivity(), false, 0);

                    pullToRefreshListView.doPullRefreshing(true, 500);

                    setLastUpdateTime();

                }

            }
        });

        show_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                startActivity(new Intent(getActivity(), GoodsInfoActivity.class).putExtra("id", show_lists.get(position).id).putExtra("region_id", mCity_id));


            }
        });

        show_choose_city_ll.setOnClickListener(this);

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                show_lists.clear();
                showListGet.page = 1;
                showListGet.execute(getActivity(), false, 1);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                if (showList != null && showList.total != show_lists.size()) {

                    showListGet.page = showList.current_page + 1;

                    showListGet.execute(getActivity(), false, 1);

                } else {

                    pullToRefreshListView.onPullUpRefreshComplete();

                    pullToRefreshListView.onPullDownRefreshComplete();

                }

            }

        });

        setLastUpdateTime();

        setAppCallBack(new setCityNameIdCallback());

        showTypeListGet.execute(getActivity(), false);

        return view;

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.show_choose_city_ll:

                startActivity(new Intent(getActivity(), ChooseCityActivity.class));

                break;

        }

    }

    private String mCity_id;

    public class setCityNameIdCallback implements AppCallBack {

        public void setCityNameId(String cityName, String cityId) {

            Http.getInstance().show(getActivity());

            show_choose_city_tv.setText(cityName);

            show_lists.clear();
            showListGet.region_id = BaseApplication.BasePreferences.getCityId();
            mCity_id = cityId;
            showListGet.page = 1;
            showListGet.execute(getActivity(), false);

        }

        public void refresh() {

            pullToRefreshListView.doPullRefreshing(true, 500);
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        is_live = false;

    }
}
